<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:include page="global.jsp"></jsp:include>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>人员代理管理</title>
<style type="text/css">
.ctl{
	text-decoration: none;
	color: blue;
	margin: auto 7px;
}
.ctl:HOVER{
	text-decoration: underline;
}
.center{
	text-align: center;
}
</style>
</head>
<body>
	<table id="dg" class="easyui-datagrid" title="人员代理管理"
			data-options="
				rownumbers:true,
				iconCls: 'icon-edit',
				singleSelect: true,
				toolbar: '#tb',
				singleSelect:true,
				pagination:true,
				fit:true,
				fitColumns:true,
				pageList:[20,25,30,35,40,45,50],
				pageSize:20
			">
		<thead>
			<tr>
				<th data-options="field:'agentName',width:200,align:'center'">代理人姓名</th>
				<th data-options="field:'personName',width:200,align:'center'">被代理人姓名</th>
				<th data-options="field:'startDate',width:100,align:'center'">开始日期</th>
				<th data-options="field:'endDate',width:100,align:'center'">结束日期</th>
				<th data-options="field:'status',width:60,align:'center'">状态</th>
				<th data-options="field:'withdrawDate',width:100,align:'center'">收回日期</th>
				<th data-options="field:'id',width:50,align:'center',formatter:formatControl">管理</th>
			</tr>
		</thead>
	</table>

	<div id="tb" style="height:auto">
		<div>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="clearForm('form', 'addItem')">新增</a>
		</div>
		<div>
			代理人姓名:
			<select data-options="editable: false" id = "agent" class="easyui-combobox">
				<option value = ""> --全部-- </option>
				<c:forEach var="item" items="${persons }" >
					<option value = "${item.id }">${item.name }</option>
				</c:forEach>
			</select>
			日期范围: <input class="easyui-datebox" type="text" id="startDate" style="width:100px;height:25px;">
			~ <input class="easyui-datebox" type="text" id="endDate" style="width:100px;height:25px;">
			<a href="javascript:search()" class="easyui-linkbutton" iconCls="icon-search">查 找</a>
		</div>
	</div>
	
	<div id="addItem" class="easyui-window" title="新增人员代理" data-options="modal:true,closed:true,iconCls:'icon-search'" style="width:670px; height:240px;padding:5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding:10px;">
				<form id="form" method="post" action="editPersonProxy" style = "width:100%; height: 100%">
					<table width="100%" height="100%">
						<tr>
							<td width="150px;" align="right">被代理人姓名：</td>
							<td width="160px">
								<select name = "person.id" class="easyui-combobox" data-options="required:true, editable: false">
									<c:forEach var="item" items="${persons }" >
										<option value = "${item.id }">${item.name }</option>
									</c:forEach>
								</select>
							</td>
							<td width="130px;" align="right">代理人姓名：</td>
							<td>
								<select name = "agent.id" class="easyui-combobox" data-options="required:true, editable: false">
									<c:forEach var="item" items="${persons }" >
										<option value = "${item.id }">${item.name }</option>
									</c:forEach>
								</select>
							</td>
						</tr>
						<tr>
							<td align="right">开始日期：</td>
							<td>
								<input class="easyui-datebox" name="startDate" data-options="required:true" style="width: 200px;" />
							</td>
							<td align="right">结束日期：</td>
							<td>
								<input class="easyui-datebox" name="endDate" data-options="required:true" style="width: 200px;" />
							</td>
						</tr>
						<tr>
							<td align="right">状态：</td>
							<td>
								<input class="easyui-textbox" name="status" data-options="required:true" style="width: 200px;" />
							</td>
							<td align="right">收回日期：</td>
							<td>
								<input class="easyui-datebox" name="withdrawDate" style="width: 200px;" />
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="$('#form').submit()" style="width:80px">确 定</a>
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="javascript:$('#addItem').window('close')" style="width:80px">关 闭</a>
			</div>
		</div>
	</div>
	
	<div id="editItem" class="easyui-window" title="编辑人员代理" data-options="modal:true,closed:true,iconCls:'icon-search'" style="width:670px; height:300px;padding:5px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding:10px;">
				<form id="editForm" method="post" action="editPersonProxy" style = "width:100%; height: 100%">
					<input type="hidden" id="editId" name = "id">
					<input type="hidden" id="editDateCreated" name = "dateCreated">
					<table width="100%" height="100%">
						<tr>
							<td width="150px;" align="right">被代理人姓名：</td>
							<td width="160px">
								<select name = "person.id" id="editPersonId" class="easyui-combobox" data-options="required:true, editable: false">
									<c:forEach var="item" items="${persons }" >
										<option value = "${item.id }">${item.name }</option>
									</c:forEach>
								</select>
							</td>
							<td width="130px;" align="right">代理人姓名：</td>
							<td>
								<select name = "agent.id" id="editAgentId" class="easyui-combobox" data-options="required:true, editable: false">
									<c:forEach var="item" items="${persons }" >
										<option value = "${item.id }">${item.name }</option>
									</c:forEach>
								</select>
							</td>
						</tr>
						<tr>
							<td align="right">开始日期：</td>
							<td>
								<input class="easyui-datebox" name="startDate" id="editStartDate" data-options="required:true" style="width: 200px;" />
							</td>
							<td align="right">结束日期：</td>
							<td>
								<input class="easyui-datebox" name="endDate" id="editEndDate" data-options="required:true" style="width: 200px;" />
							</td>
						</tr>
						<tr>
							<td align="right">状态：</td>
							<td>
								<input class="easyui-textbox" name="status" id="editStatus" data-options="required:true" style="width: 200px;" />
							</td>
							<td align="right">收回日期：</td>
							<td>
								<input class="easyui-datebox" name="withdrawDate" id="editWithdrawDate" style="width: 200px;" />
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="$('#editForm').submit()" style="width:80px">确 定</a>
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="javascript:$('#editItem').window('close')" style="width:80px">关 闭</a>
			</div>
		</div>
	</div>
	
	<script type="text/javascript">
		function formatControl(val, row){
			return '<a class="ctl" href="javascript:void(0)" onclick="editItem(' + val + ')">编辑</a>'+
			' <a class="ctl" href="javascript:void(0)" onclick="delItem(' + val + ')">删除</a>';
		}
		function search(){
			var dg = $("#dg");
			//var opts = dg.datagrid('options');
			var agent = $("#agent").combobox("getValue");
			var startDate = $("#startDate").textbox("getValue").trim();
			var endDate = $("#endDate").textbox("getValue").trim();
			var url = "queryPersonProxy?agentId=" + agent + "&startDate=" + startDate + "&endDate=" + endDate;
			dg.datagrid('reload', url);
		}
		function editItem(id){
			var data = "id=" + id;
			$.ajax({
			  type: "GET",
			  url: "findPersonProxy",
			  data: data,
			  success:function(data){
				  	var val = $.parseJSON(data);
					if(val.success){
						var item = val.data;
						
						$("#editId").val(id);
						$("#editDateCreated").val(item.dateCreated);
						$("#editPersonId").combobox("setValue", item.personId);
						$("#editAgentId").combobox("setValue", item.agentId);
						$("#editStartDate").textbox("setValue", item.startDate);
						$("#editEndDate").textbox("setValue", item.endDate);
						$("#editStatus").textbox("setValue", item.status);
						$("#editWithdrawDate").textbox("setValue", item.withdrawDate);
						
						$('#editItem').window('open');
					}else{
						$.messager.alert("提示", val.msg, "error");
					}
			  	}
			});
		}
		function delItem(id){
			$.messager.confirm("删除确认", "您确定要删除所选的人员代理吗？", function(r){
				if(r){
					data = "id=" + id;
					$.ajax({
					  type: "GET",
					  url: "delPersonProxy",
					  data: data,
					  success:function(data){
						  	var val = $.parseJSON(data);
							if(val.success){
								bottomRight('提示', '操作成功！');
								$('#dg').datagrid('reload');
							}else{
								$.messager.alert("提示", val.msg, "error");
							}
					  	}
					});
				}
			});
		}
		$(function(){
			$("#form").form({
				onSubmit: function(){
					var isValid = $(this).form('validate');
					return isValid;	// 返回false将停止form提交 
			    },
				success:function(data){
					var val = $.parseJSON(data);
					if(val.success){
						$('#dg').datagrid('reload');
						bottomRight('提示', '操作成功！');
						$('#addItem').window('close');
					}else{
						$.messager.alert("提示", val.msg, "error");
					}
				}
			});
			$("#editForm").form({
				onSubmit: function(){
					var isValid = $(this).form('validate');
					return isValid;	// 返回false将停止form提交 
			    },
				success:function(data){
					var val = $.parseJSON(data);
					if(val.success){
						$('#dg').datagrid('reload');
						bottomRight('提示', '操作成功！');
						$('#editItem').window('close');
					}else{
						$.messager.alert("提示", val.msg, "error");
					}
				}
			});
			//validateMenu
			search();//页面装载完成后加载数据
		});
	</script>
</body>
</html>